import QtQuick 2.0
import DataMan 0.1
import "../logic.js" as Logic;
import "../utils.js" as Utils;

Item {
	id: root
	property int tp: DataMan.MNONE;
	property string imgsource: ""
	property int st: Logic.MonSt.Normal;
	property int moveCnt: 0
	property var moveCB: null
	width: parent.width / GameCol
	height: parent.height / GameRow

	transform: [
		Scale {
			id: rootScale
			xScale: Utils.DesiginScaleFactor.x
			yScale: Utils.DesiginScaleFactor.x
			origin.y: root.height
		}
	]

	// 配置相应的动画播放
	onStChanged: {
		switch(st) {
		case Logic.MonSt.Normal:
			markedAni.visible = false;
			sp.jumpTo("normal");
			break;
		case Logic.MonSt.Marked:
			markedAni.visible = true;
			jumpAni.stop();
			sp.jumpTo("marked");
			break;
		case Logic.MonSt.Dead:
			markedAni.visible = false;
			sp.jumpTo("deaded");
			// 播放死亡动画
			deadAni.start();
			break;
		}
	}

	// 配置小怪的颜色
	onTpChanged: {
		if (DataMan.GNONE === tp) {
			root.visible = false;
			return;
		}
		root.visible = true;
		switch(tp) {
		case DataMan.MG:
			imgsource = "image://default/mon/g.png";
			return;
		case DataMan.MR:
			imgsource = "image://default/mon/r.png";
			return;
		case DataMan.MB:
			imgsource = "image://default/mon/b.png";
			return;
		case DataMan.MP:
			imgsource = "image://default/mon/p.png";
			return;
		case DataMan.MY:
			imgsource = "image://default/mon/y.png";
			return;
		}
	}

	SpriteSequence {
		id: sp
		width: 68
		height: 107
		x: (root.width - width) / 2
		y: (root.height - height)

		Sprite {
			name: "normal"
			source: imgsource
			frameCount: 1
			frameWidth: sp.width
			frameHeight: sp.height
			frameDuration: 1000
			to: {"blink": 1, "normal": 9}
		}

		Sprite {
			name: "blink"
			source: imgsource
			frameCount: 2
			frameWidth: sp.width
			frameHeight: sp.height
			frameDuration: 100
			to: {"normal": 4, "blink": 1}
		}

		Sprite {
			name: "marked"
			source: imgsource
			frameY: sp.height
			frameCount: 1
			frameWidth: sp.width
			frameHeight: sp.height
		}

		Sprite {
			name: "dead"
			source: imgsource
			frameCount: 1
			frameX: sp.width
			frameY: sp.height
			frameWidth: sp.width
			frameHeight: sp.height
		}

	}

	// 上下跳
	SequentialAnimation {
		id: jumpAni
		running: false

		ParallelAnimation {
			PropertyAnimation {
				target: rootScale
				property: "yScale"
				duration: 200
				to: 1.3 * Utils.DesiginScaleFactor.x
			}
			PropertyAnimation {
				target: rootScale
				property: "xScale"
				easing.type: Easing.OutQuad
				duration: 200
				to: 1.03 * Utils.DesiginScaleFactor.x
			}
		}
		ParallelAnimation {
		    PropertyAnimation {
			    target: rootScale
			    property: "yScale"
			    easing.amplitude: 2
			    easing.type: Easing.OutBounce
			    duration: 500
			    to: Utils.DesiginScaleFactor.x
		    }
			PropertyAnimation {
				target: rootScale
				property: "xScale"
				duration: 200
				to: Utils.DesiginScaleFactor.x
			}
		}

		PauseAnimation {
			duration: 500
		}
	}

	Timer {
		id: funnyTimer
		interval: 250
		running: true
		repeat: true
		onTriggered: {
			if (Logic.MonSt.Normal === st) {
				var k = Math.floor(Math.random() * 200);
				if (k < 1) {
					if (!jumpAni.running)
						jumpAni.start();
				}
			}
		}
	}

	onVisibleChanged: {
		if (visible) {
			funnyTimer.start();
		} else {
			funnyTimer.stop();
		}
	}

	AnimatedSprite {
		id: markedAni
		visible: false
		width: 65
		height: 49
		y: -25
		x: -2
		source: "image://default/light.png"
		frameCount: 3
		frameWidth: width
		frameHeight: height
		frameDuration: 100
		onVisibleChanged: {
			running = visible;
		}
	}

	PropertyAnimation {
		id: deadAni
		target: sp
		running: false
		property: "y"
		duration: 500
		to: sp.y - Utils.AppWin.height
		onStopped: root.destroy();
	}

	SequentialAnimation {
		property real dest: 0
		id: ymoveAni
		running: false
		// TODO:
		ParallelAnimation {
			PropertyAnimation {
				target: rootScale
				property: "yScale"
				to: Utils.DesiginScaleFactor.y * 1.5
				duration: 50
			}
			PropertyAnimation {
				target: root
				property: "y"
				to: ymoveAni.dest / 3 + root.y
				duration: 50
			}
		}
		PropertyAnimation {
			target: root
			property: "y"
			to: ymoveAni.dest + root.y
			duration: 100
		}
		PropertyAnimation {
			target: rootScale
			property: "yScale"
			easing.type: Easing.OutBack
			to: Utils.DesiginScaleFactor.y
			duration: 100
		}
		onStopped: {
			--moveCnt;
			if (0 === moveCnt && moveCB)
				moveCB();
		}
	}

	function dropY(y, cb)
	{
		moveCnt = 0;
		moveCB = cb;
		if (y !== 0) {
			ymoveAni.dest = y;
			ymoveAni.start();
			++moveCnt;
		}
	}

	ParallelAnimation {
		property real stepL: 1
		id: walkAni
		running: false

		PropertyAnimation {
				target: root
				property: "x"
				easing.type: Easing.InOutQuad
				duration: 200
				to: root.x + walkAni.stepL
		}
		SequentialAnimation {
			PropertyAnimation {
				target: root
				property: "y"
				easing.type: Easing.OutQuad
				duration: 100
				to: root.y - 30
			}
			PropertyAnimation {
				target: root
				property: "y"
				easing.type: Easing.InQuad
				duration: 100
				to: root.y
			}
		}
		SequentialAnimation {
			PropertyAnimation {
				target: root
				property: "rotation"
				duration: 30
				to: root.rotation + (walkAni.stepL > 0 ? 30 : -30)
			}
			PropertyAnimation {
				target: root
				property: "rotation"
				duration: 40
				to: root.rotation + (walkAni.stepL > 0 ? -20 : 20)
			}
			PropertyAnimation {
				target: root
				property: "rotation"
				duration: 30
				to: root.rotation
			}
			PauseAnimation {
				duration: 100
			}
		}

		SequentialAnimation {
			PropertyAnimation {
				target: rootScale
				property: "yScale"
				easing.overshoot: 2.5
				easing.type: Easing.InBack
				duration: 100
				to: Utils.DesiginScaleFactor.y * 1.1
			}

			PropertyAnimation {
				target: rootScale
				property: "yScale"
				easing.overshoot: 2.5
				easing.type: Easing.OutBack
				duration: 100
				to: Utils.DesiginScaleFactor.y
			}
		}

		onStopped: {
			--moveCnt;
			if (moveCnt > 0) {
				start();
			} else {
				z = 0;
				if (moveCB)
					moveCB();
			}
		}
	}

	// 横着用step步走到x位置
	function walkByX(x, step, cb)
	{
		if (0 === x)
			return;
		if (step < 1)
			step = 1;
		z = 1;
		moveCB = cb;
		moveCnt = step;
		walkAni.stepL = x;
		walkAni.running = true;
	}

}

